package com.easepal7506a.project.ui.presenter;


import com.easepal.socketiolib.model.SocketModel;
import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.SocketDataObserver;
import com.easepal7506a.project.ui.iview.IProfessionalMassageView7506;
import com.ogawa.base.utils.CommonUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Yang on 2017/12/13.
 */

public class ProfessionalMassagePresenter7506 implements SocketDataObserver.SocketReceive {
    IProfessionalMassageView7506 mView;
    private Disposable mDisposable;


    public ProfessionalMassagePresenter7506(IProfessionalMassageView7506 view) {
        mView = view;
        SocketDataObserver.getInst().registerObserver(this);
    }
    //全身舒展	1 老年模式2泰式拉伸	3速按放松	4清晨唤醒	5
    // 舒眠模式	6运动恢复	7深度放松	8 上班族	9低头族	10开车族	11高跟族	12午间小休	13女王美体	14男士健体	15久坐释压	16
    public void initData(){
        String name = "";
        if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("9")){
            mView.noticeChange(0,0);
            name = "上班族";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("10")){
            mView.noticeChange(0,1);
            name = "低头族";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("11")){
            mView.noticeChange(0,2);
            name = "开车族";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("12")){
            mView.noticeChange(0,3);
            name = "高跟族";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("5")){
            mView.noticeChange(1,0);
            name = "清晨唤醒";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("13")){
            mView.noticeChange(1,1);
            name = "午间小休";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("6")){
            mView.noticeChange(1,2);
            name = "舒眠模式";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("14")){
            mView.noticeChange(2,0);
            name = "女王美体";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("15")){
            mView.noticeChange(2,1);
            name = "男士健体";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("2")){
            mView.noticeChange(2,2);
            name = "老年模式";
        }

        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("4")){
            mView.noticeChange(3,0);
            name = "速按放松";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("8")){
            mView.noticeChange(3,1);
            name = "深度放松";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("16")){
            mView.noticeChange(3,2);
            name = "久坐释压";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("1")){
            mView.noticeChange(4,0);
            name = "全身舒展";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("7")){
            mView.noticeChange(4,1);
            name = "运动恢复";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("3")){
            mView.noticeChange(4,2);
            name = "泰式拉伸";
        }
        else if(DataManager.getInst().getValue(CommmandNum.mStatusAuto).equals("0")){
            mView.noticeChange(-1,-1);
            name = "";
        }

        mView.getName(name);


    }
    private void toActivity(){
        Observable.timer(200, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                        mDisposable =d;
                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {
                        String hand = DataManager.getInst().getValue(CommmandNum.mStatusHand);
                        String knead = DataManager.getInst().getValue(CommmandNum.mStatusKnead);
                        String auto = DataManager.getInst().getValue(CommmandNum.mStatusAuto);
                        if(auto.equals("0")){
                            if(!hand.equals("0")||!knead.equals("0")){
                                mView.startAdvance();
                            }
                            else {
//                                mView.closeActivity();

                            }
                        }

                        mDisposable.dispose();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    @Override
    public void onRceiveSocket(SocketModel model) {
        String value = model.getValue();
        if(model.getKey().equals(CommmandNum.mStatusAuto)){
            if(!value.equals("0")){
                mView.startDetail(CommonUtil.getAutoName(value));
            }
            initData();
        }
        else if(model.getKey().equals(CommmandNum.mStatusHand)){
            if(!value.equals("0")){
                toActivity();
            }

        }
        else if(model.getKey().equals(CommmandNum.mStatusKnead)){
            if(!value.equals("0")){
                toActivity();
            }
        }
        else if(model.getKey().equals(CommmandNum.mStatusRunning)){
            mView.isPause(value.equals("3"));
        }
    }
    public void onDestory(){
        SocketDataObserver.getInst().removeObserver(this);
    }
}
